package com.digitalchina.trade.service.impl;

import com.digitalchina.trade.entity.TbAccount;
import com.digitalchina.trade.mapper.AccountMapper;
import com.digitalchina.trade.service.AccountService;
import com.digitalchina.trade.utils.CodeUtil;
import com.digitalchina.trade.utils.Md5Util;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountMapper accountMapper;
    @Override
    public TbAccount login(@Param("account") TbAccount tbAccount) {
        return accountMapper.login(tbAccount);
    }
    @Override
    public String saveAccount(String account_name,String account_pwd,String account_upwd,
                              String  account_cradNum,String account_email,String account_phoneNum) {
        TbAccount account = new TbAccount();
        String msg = "注册失败";
        if (Objects.equals(account_pwd,account_upwd)){
            if (accountMapper.selectByAccountName(account_name)!=null){
                msg = "账户名已存在，请更改账户名";
                return msg;
            }else if (accountMapper.selectByAccountCradNum(account_cradNum)!=null){
                msg = "身份证号已被他人绑定";
                return msg;
            }else if (accountMapper.selectByAccountEmail(account_email)!=null){
                msg = "邮箱已被他人绑定";
                return msg;
            }else if (accountMapper.selectByAccountPhoneNum(account_phoneNum)!=null){
                msg = "电话号码已被他人绑定";
                return msg;
            }else{
                account.setAccount_name(account_name);
                account.setAccount_cradNum(account_cradNum);
                account.setAccount_pwd(Md5Util.encode(account_pwd));
                account.setAccount_email(account_email);
                account.setAccount_phoneNum(account_phoneNum);
                account.setAccount_num(CodeUtil.getRandom(9));
                if (accountMapper.saveAccount(account)>0){
                    msg = "注册成功";
                    return msg;
                }
            }
        }else{
            return msg;
        }
        return null;
    }

    @Override
    public void updateAccount(TbAccount account) {
        accountMapper.updateAccount(account);
    }

}
